package ln.fxspringarchetype.controller.course;

import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.util.converter.NumberStringConverter;
import ln.fxspringarchetype.controller.BaseFxController;
import ln.fxspringarchetype.vo.CourseVO;
import org.apache.logging.log4j.util.Strings;

import java.util.Random;

public class AddCourseController extends BaseFxController {
    /**
     * 表单对应的视图模型
     */
    private final CourseVO courseVO = new CourseVO();
    @FXML
    private TextField descriptionField;
    @FXML
    private TextField creditField;
    @FXML
    private TextField nameField;

    public CourseVO getCourseVO() {
        return courseVO;
    }

    @Override
    public void initLayout() {
        nameField.setText("课程名称");
        creditField.setText("0.0");
        descriptionField.setText(Strings.EMPTY);
    }

    @Override
    public void loadData() {
        courseVO.setId(new Random().nextInt(0, Integer.MAX_VALUE));
    }

    @Override
    public void setCellValueFactories() {

    }

    @Override
    public void setCellFactories() {

    }

    @Override
    public void createBindings() {
        //绑定控件的value属性到VO
        nameField.textProperty().bindBidirectional(courseVO.nameProperty());
        descriptionField.textProperty().bindBidirectional(courseVO.descriptionProperty());
        //将学分与文本框进行双向绑定，注意这里是NumberStringConverter
        Bindings.bindBidirectional(creditField.textProperty(), courseVO.creditProperty(), new NumberStringConverter());
    }
}
